package com.example.qu_tao.View;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.example.qu_tao.R;
import com.example.qu_tao.adapter.FootprintAdapter;
import com.example.qu_tao.database.DBHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZuJiActivity extends AppCompatActivity {

    private ListView listViewHistory;
    private LinearLayout layoutEmpty;
    private Button btnDeleteAll;
    private Button btnStartShopping;
    private ImageView backbutton;
    private DBHelper dbHelper;
    private List<Map<String, Object>> footprintDataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zuji);

        listViewHistory = findViewById(R.id.list_view_history);
        layoutEmpty = findViewById(R.id.layout_empty);
        btnDeleteAll = findViewById(R.id.btn_delete_all);
        btnStartShopping = findViewById(R.id.btn_start_shopping);
        backbutton=findViewById(R.id.back_button);


        dbHelper = new DBHelper(this);

        // 加载足迹数据
        loadFootprintData();

        // 设置清空全部按钮点击事件
        btnDeleteAll.setOnClickListener(v -> {
            clearFootprintData();
            loadFootprintData();
        });

        // 设置去逛逛按钮点击事件
        btnStartShopping.setOnClickListener(v -> {
            // 这里可以添加跳转到首页的逻辑
            Intent intent = new Intent(ZuJiActivity.this, MainActivity.class);
            startActivity(intent);
        });
        // 设置返回按钮点击事件
        backbutton.setOnClickListener(v -> {
            finish(); // 关闭当前活动，返回上一个活动
        });
        // 设置列表项点击事件 footprintList
        listViewHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 点击足迹商品跳转到商品详情页
                int goodsId = (int) footprintDataList.get(position).get("goods_id");
                Intent intent = new Intent(ZuJiActivity.this, GoodsDetailActivity.class);
                intent.putExtra("goodsId", goodsId);
                startActivity(intent);
            }
        });
    }
    public void updateEmptyViewVisibility(boolean isEmpty) {
        if (isEmpty) {
            layoutEmpty.setVisibility(View.VISIBLE);
            listViewHistory.setVisibility(View.GONE);
        } else {
            layoutEmpty.setVisibility(View.GONE);
            listViewHistory.setVisibility(View.VISIBLE);
        }
    }

    @SuppressLint("Range")
    private void loadFootprintData() {
        footprintDataList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_NAME_FOOTPRINT, null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                Map<String, Object> map = new HashMap<>();
                map.put("goods_id", cursor.getInt(cursor.getColumnIndex("goods_id")));
                map.put("goods_name", cursor.getString(cursor.getColumnIndex("goods_name")));
                map.put("goods_price", cursor.getDouble(cursor.getColumnIndex("goods_price")));
                map.put("goods_address", cursor.getString(cursor.getColumnIndex("goods_address")));
                map.put("goods_content", cursor.getString(cursor.getColumnIndex("goods_content")));
                footprintDataList.add(map);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();

        if (footprintDataList.isEmpty()) {
            layoutEmpty.setVisibility(View.VISIBLE);
            listViewHistory.setVisibility(View.GONE);
        } else {
            layoutEmpty.setVisibility(View.GONE);
            listViewHistory.setVisibility(View.VISIBLE);
            FootprintAdapter adapter = new FootprintAdapter(this, footprintDataList);
            listViewHistory.setAdapter(adapter);
        }
    }

    private void clearFootprintData() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(DBHelper.TABLE_NAME_FOOTPRINT, null, null);
        db.close();
    }
}